//index.js
const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')
const utils = require('../../utils.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    allCLassState:false,
    classInfoDTOs:[],
    clockList:[],
    currentClass:{},
    pageSize:10,
    pageNum:1,
    offsetH:0,
    ouId:'',
    ciId:''
  },

  // 班级切换操作
  classSwitch(e) {
    this.setData({
      allCLassState: !this.data.allCLassState
    })
    
    
  },

  //获取班级打卡
  obtainCardsForClass() {
    utils.ajaxFn({
      url: config.service.obtainCardsForUser,
      body: {
        ouId: this.data.uId,
        pageSize: this.data.pageSize,
        pageNum: this.data.pageNum,
        ciId: this.data.currentClass['ciId']
      },
      callback: (data) => {
        if (data.cardDTOs.length <= 0) {
          wx.showToast({
            title: '当前班级无打卡信息！',
            image: "../../images/error.png",
            icon: 'none',
            duration: 2000
          })
          return false;
        }

        this.processingDate(data)
      }
    })
  },

  // 班级打卡信息处理
  processingDate(data) {
    let obj = {
      userHeader: [],
      date: '',
      iarr: []
    }

    data.cardDTOs.forEach((value) => {
      var data = new Date(value.createTime);
      (obj.userHeader.indexOf(value.user.avatarUrl) === -1) && (obj.userHeader = obj.userHeader.concat(value.user.avatarUrl))
      obj.date == '' && (obj.date = `${this.zeroize(data.getMonth() + 1)}月${this.zeroize(data.getDate())}日`)
      obj.dateFormat = value.createTime
      obj.iarr.push(value)
    })

    // 传参数据
    this.setData({
      clockList: this.data.clockList.concat(obj)
    })

    

    //处理数据不够的情况下
    if (this.judgeData(this.data.clockList) < 3) {
      let endDate = this.data.clockList[this.data.clockList.length - 1]

      this.obtainCardsForClass(this.data.currentClass.ciId, utils.formatDate(endDate.dateFormat, 1))
    }

  },

  // 全局捕获
  bindViewTap(event) {
    if (event.target.id != 'classId') {
      this.selectComponent("#classId").hideFn()
    }
  },
  
  // 不零操作数据
  zeroize(value) {
    return value < 9 ? `0${value}` : value
  },

  // 处理数据达到不3条自动加载下一天数据（不满足下拉加载）
  judgeData(data) {
    var iarr = data.map((value) => {
      return value.iarr.length
    })

    return iarr.reduce((newValue, oldValue) => newValue + oldValue)
  },

  // 滚动加载
  lower() {
    if (!this.data.hasCard) {
      return;
    }
    this.pageNum ++

    this.obtainCardsForClass()
  },

  // 切换班级
  switchClass(value) {
    let curData = value.detail.currentClass
    

   

    var classlist = {
      name: curData.name,
      ciId: curData.ciId,
      groups: curData.groups,
      isMember: curData.isMember
    }

    this.setData({
      currentClass: classlist
    })

    this.setData({
      clockList: []
    })

    this.setData({
      pageNum: 1
    })

    this.obtainCardsForClass()
  },


  // 获取班级信息
  getobtainClassInfos() {
    utils.ajaxFn({
      url: config.service.obtainClassInfos,
      callback: (data) => {
        this.setData({
          classInfoDTOs: data.classInfoDTOs
        })

        var classlist = {
          name: this.data.classInfoDTOs[0].name,
          ciId: this.data.classInfoDTOs[0].ciId,
          groups: this.data.classInfoDTOs[0].groups,
          isMember: this.data.classInfoDTOs[0].isMember
        }

        this.setData({
          currentClass: classlist
        })

        this.obtainCardsForClass()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // uId
    this.setData({
      uId: options.uId
    })
    this.getobtainClassInfos()

    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          offsetH: res.windowHeight
        })
      }
    })
  }
  
})